﻿//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Expenses.ViewModels;
using Expenses.ExpenseService;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using Windows.ApplicationModel.DataTransfer;
using System.Text;
using Windows.Storage.Streams;
using Windows.UI.StartScreen;
using Windows.UI.ViewManagement;

namespace Expenses.Views
{
    public sealed partial class ExpenseReportsPage : Expenses.Common.LayoutAwarePage
    {
        ExpenseReportsViewModel expenseReportsViewModel = null;
        string reportTypes = string.Empty;
        private DataTransferManager dataTransferManager = null;
        string eventArgsParameter = string.Empty;

        public ExpenseReportsPage()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            eventArgsParameter = (string)e.Parameter;
            reportTypes = eventArgsParameter;
            GetExpenseReports();

            // Register handler for DataRequested events for sharing
            dataTransferManager = DataTransferManager.GetForCurrentView();
            dataTransferManager.DataRequested += new TypedEventHandler<
                DataTransferManager, DataRequestedEventArgs>(OnDataRequested);
        }

        private async void GetExpenseReports()
        {
            ExpenseReportsProgressRing.IsActive = true;

            expenseReportsViewModel = new ExpenseReportsViewModel();
            var expenseReports = new ObservableCollection<ExpenseReportViewModel>();
            switch (reportTypes)
            {
                case "Saved Reports":
                    expenseReports = await
                        expenseReportsViewModel.GetSavedExpenseReports();
                    ExpenseReportsGridView.ItemTemplate = (DataTemplate)
                        (this.Resources["SavedExpenseReportTemplate"]);
                    ExpenseReportsListView.ItemTemplate = (DataTemplate)
                        (this.Resources["SnappedSavedExpenseReportTemplate"]);
                    break;
                case "Pending Reports":
                    expenseReports = await
                        expenseReportsViewModel.GetPendingExpenseReports();
                    ExpenseReportsGridView.ItemTemplate = (DataTemplate)
                        (this.Resources["PendingExpenseReportTemplate"]);
                    ExpenseReportsListView.ItemTemplate = (DataTemplate)
                        (this.Resources["SnappedPendingExpenseReportTemplate"]);
                    break;
                case "Needing Approval":
                    expenseReports = await
                        expenseReportsViewModel.GetWaitingForApprovalExpenseReports();
                    ExpenseReportsGridView.ItemTemplate = (DataTemplate)
                        (this.Resources["NeedingApprovalExpenseReportTemplate"]);
                    ExpenseReportsListView.ItemTemplate = (DataTemplate)
                        (this.Resources["SnappedNeedingApprovalExpenseReportTemplate"]);
                    break;
                default:
                    return;
            }
            ExpenseReportsViewSource.Source = expenseReports;

            decimal amountOfExpenseReports = Convert.ToDecimal(
                                               (from report in expenseReports
                                                select report.Amount).Sum());

            if (amountOfExpenseReports > 0)
            {
                PageTitleTextBlock.Text = string.Format(
                    "{0} ({1} for {2:C})", reportTypes,
                    expenseReports.Count(), amountOfExpenseReports);
            }
            else
            {
                PageTitleTextBlock.Text = string.Format(
                    "You have no {0}", reportTypes.ToLower());
            }
            ExpenseReportsProgressRing.IsActive = false;
        }

        private void ExpenseReportsGridView_ItemClick(object sender, ItemClickEventArgs e)
        {
            App.CurrentExpenseReport =
                (ExpenseReportViewModel)e.ClickedItem;
            this.Frame.Navigate(typeof(ExpenseReportPage));
        }

        private void BackButton_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(MainPage));
        }

        void OnDataRequested(DataTransferManager sender, DataRequestedEventArgs args)
        {
            var request = args.Request;
            request.FailWithDisplayText("This page does not support sharing.");
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            dataTransferManager.DataRequested -= new TypedEventHandler<
                DataTransferManager, DataRequestedEventArgs>(OnDataRequested);
            switch (eventArgsParameter)
            {
                case "Saved Reports":
                    App.LastPage = "Saved Reports";
                    break;
                case "Pending Reports":
                    App.LastPage = "Pending Reports";
                    break;
                case "Needing Approval":
                    App.LastPage = "Needing Approval";
                    break;
            }
        }

        public static Rect GetElementRect2(FrameworkElement element)
        {
            GeneralTransform buttonTransform = element.TransformToVisual(null);
            Point point = buttonTransform.TransformPoint(new Point());
            return new Rect(point, new Size(element.ActualWidth, element.ActualHeight));
        }

    }
}
